package cn.seqdata.jdbc;

import org.joda.time.*;

import java.util.Date;
import java.util.Objects;

public final class JodaUtils {
	private JodaUtils() {
	}

	public static java.sql.Timestamp toTimestamp(ReadableDateTime occur) {
		return new java.sql.Timestamp(occur.getMillis());
	}

	public static java.sql.Timestamp toTimestamp(LocalDateTime occur) {
		return toTimestamp(occur.toDateTime());
	}

	public static java.sql.Date toDate(LocalDate date) {
		DateTime occur = date.toDateTime(LocalTime.MIDNIGHT);
		return new java.sql.Date(occur.getMillis());
	}

	public static java.sql.Time toTime(LocalTime time) {
		return new java.sql.Time(time.getMillisOfDay());
	}

	public static DateTime toDateTime(Date val) {
		return Objects.nonNull(val) ? new DateTime(val.getTime()) : null;
	}

	public static LocalDateTime toLocalDateTime(Date val) {
		return Objects.nonNull(val) ? LocalDateTime.fromDateFields(val) : null;
	}

	public static LocalDate toLocalDate(Date val) {
		return Objects.nonNull(val) ? LocalDate.fromDateFields(val) : null;
	}

	public static LocalTime toLocalTime(Date val) {
		return Objects.nonNull(val) ? LocalTime.fromDateFields(val) : null;
	}
}
